/**
 * File : TCPClient.java Author : R. Scheurer (EIA-FR) Date : 04.10.2012
 *
 * Description - a simple TCP client
 *
 */
package sockets.tcp;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;

// A class to implement the TCP client's behaviour 
// and interface the provided GUI
public class TCPClient {

    private Socket s; // The communication's socket
    private ObjectOutputStream oos; // The communication's output stream object

    // The class' constructor taking one parameter :
    // - The socket's INet Adress
    public TCPClient(InetSocketAddress a) throws SocketException, IOException {
        s = new Socket(a.getAddress(), a.getPort());
        // If the socket is not connected yet, we start its set the connection.
        if (!s.isConnected()) {
            s.connect(a);
        }
        // We create the Stream Output Object with the connected socket we have
        oos = new ObjectOutputStream(s.getOutputStream());
    }

    // A method to close the TCP connction socket
    public void closeSocket() throws IOException {
        s.close();
    }

    // A method to send a message from the TCP client
    public void sendMsg(String msg) throws IOException {
        oos.writeObject(msg);
    }
}
